package leetcode;

import java.util.Stack;

public class p232用栈实现队列 {
    /** Initialize your data structure here. */
    private Stack<Integer> stack1=new Stack<>();//出数据
    private Stack<Integer> stack2=new Stack<>();//进数据
    public p232用栈实现队列() {

    }

    /** Push element x to the back of queue. */
    public void push(int x) {
        stack2.push(x);
    }

    /** Removes the element from in front of queue and returns that element. */
    public int pop() {//弹出队列首元素
        //不考虑队列为空
        if(stack1.isEmpty()) {
            while (!stack2.isEmpty()){
                //进数据的stack2栈只要不为空就一直出数据进入stack1
                int e=stack2.pop();
                stack1.push(e);
            }
        }

        return stack1.pop();
    }

    /** Get the front element. */
    public int peek() {
        //不考虑队列为空
        if(stack1.isEmpty()) {
            while (!stack2.isEmpty()){
                int e=stack2.pop();
                stack1.push(e);
            }
        }

        return stack1.peek();

    }

    /** Returns whether the queue is empty. */
    public boolean empty() {
        return stack1.isEmpty()&&stack2.isEmpty();
    }

}
